package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags = "购物车接口")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;



    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加商品进入购物车：{}",shoppingCartDTO.toString());
        // 添加购物车
        shoppingCartService.add(shoppingCartDTO);
        return Result.success("添加商品成功！");
    }

    @GetMapping("/list")
    @ApiOperation("查看当前用户购物车")
    public Result<List<ShoppingCart>> showShoppingCart(){
        log.info("查询用户id的购物车:{}", BaseContext.getCurrentId());
        // 查询购物车
        List<ShoppingCart> shoppingCartList=shoppingCartService.showShoppingCart();
        log.info("查询购物车为：{}",shoppingCartList);
        return Result.success(shoppingCartList);
    }

    @DeleteMapping("/clean")
    @ApiOperation("清空当前用户购物车")
    public Result cleanShoppingCart(){
        log.info("清空用户id的购物车:{}", BaseContext.getCurrentId());
        // 查询购物车
        shoppingCartService.cleanShoppingCart();
        return Result.success("购物车已经全部清空！");
    }


}
